<?php
namespace App\HttpController\Admin;

use App\HttpController\Model\Base\NodeModel;
use App\Lib\Tool\Pwd;
use App\Repositories\Contracts\BaseOperateRepository;
use App\Repositories\Eloquent\Admin\AdminModel;
use App\Repositories\Eloquent\Admin\AdminRoleModel;
use App\Repositories\Eloquent\Admin\AdminRoleNodeModel;
use App\Repositories\Eloquent\Admin\AdminRolePartModel;
use App\Repositories\Eloquent\Admin\SysNodeModel;
use EasySwoole\Template\Render;

class Admin extends Sys implements BaseOperateRepository {

    private  $_model;
    private  $_model_role;
    private  $_model_role_party;

    public function index()
    {
        $this->show('admin/admin/index');
    }

    public function getList()
    {
        $this->_model = new AdminModel();

        $where      = [];
        if (!empty($this->_params['key'])) {
            $account = $this->_params['key']['account'];
            $name = $this->_params['key']['name'];
            $where['a.name'] = ['%' . $name . '%', 'like'];
            $where['a.account'] = ['%' . $account . '%', 'like'];
        }
        $where['a.status'] = [[0,1],'in'];
        $where['a.deleted'] = 0;
        $datas  = $this->_model->getList($where);
        $this->ajaxSuccess($datas['datas'],'',$datas['count']);


        // TODO: Implement getList() method.
    }

    public function add()
    {
        $this->show('admin/admin/add');
    }

    public function add_ajax()
    {
        $this->_model = new AdminModel();

        if (!empty($this->_params['name']) && !empty($this->_params['account']) && !empty($this->_params['password'])) {
            //验证帐号
            $check = $this->_model->getItemByAccount($this->_params['account']);
            if ($check) {
               $this->ajaxError(0,"帐号已存在");
            }
            $item['name'] = $this->_params['name'];
            $item['account'] = $this->_params['account'];
            $item['acode'] = Pwd::getAcode();
            $item['password'] = Pwd::getPass($this->_params['password'],$item['acode']);
            $item['status'] = 1;
            $reslut = $this->_model->addItem($item);
            if ($reslut) {
                $this->ajaxSuccess();
            }
        }
        $this->ajaxError();

    }

    public function edit()
    {
        $this->_model = new AdminModel();
        $param = $this->_params;
        $id = 0;
        if (!empty($param['id'])) {
            $id = $param['id'];
        } else {
           $this->ajaxError(0,"参数错误");
        }
        $item = array();
        if ($id > 0) {
            $tmp['id'] = $id;
            $item = $this->_model->getItemByid($id);
        }
        $this->show('admin/admin/edit',['item'=>$item]);

    }

    public function edit_ajax()
    {
        $this->_model = new AdminModel();
        $id = $this->_params['id'];
        if ($id > 0) {
            if (!empty($this->_params['name']) && !empty($this->_params['account']) && !empty($this->_params['password'])) {
                $password = $this->_params['password'];
                $acode = Pwd::getAcode();
                $data['name'] =$this->_params['name'];
                $data['password'] = Pwd::getPass($password, $acode);
                $data['acode'] = $acode;
                $reslut = $this->_model->updateItemById($id, $data);
                if ($reslut) {
                    $this->ajaxSuccess();
                }
            }
            $this->ajaxError(0,"参数错误");
        }
    }

    public function status()
    {
        $this->_model = new AdminModel();
        $param = $this->_params;
        if (!empty($param['ids']) && !is_null($param['status'])) {
            $ids = $param['ids'];
            //-1 删除 0禁用 1 正常启用
            $status = $param['status'];
        } else {
            $this->ajaxError(0,"参数错误");
        }
        if ($status < -2 && $status > 1) {
            $this->ajaxError(0,"参数错误");
        }
        if (!$ids) {
            $id = I('id');
            if ($id) {
                $ids [] = $id;
            }
        }
        if (!$ids) {
            $this->ajaxError(0,"您未选择任何记录");
        }
        $ids = explode(',', $ids);
        $data['status'] = $status;
        $result = $this->_model->status($ids, $data);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");
    }

    public function del()
    {
        $this->_model = new AdminModel();
        $id = $this->_params['id'];
        if (empty($id)) {
            $this->ajaxError(0,"参数错误");
        }
        $result = $this->_model->delById($id);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");

    }

    public function sort()
    {
        // TODO: Implement px() method.
    }

    function reload(){
        Render::getInstance()->restartWorker();
        $this->response()->write(1);
    }

    public function role(){

        $this->_model_role = new AdminRoleModel();
        $data['datas_roles'] =$this->_model_role->getAllList();
        $data['id_user'] = $this->_params['id_user'];
        $this->show('admin/admin/role',$data);
    }


    public function role_ajax() {

        $id_user = $this->_params['id_user'];
        $id_role = $this->_params['id_role'];

        $this->_model_role_party = new AdminRolePartModel();
        $this->_model_role_party->delRoleByAdminId($id_user);
        $data['id_admin'] =$id_user;
        $data['id_role'] =$id_role;

        $result = $this->_model_role_party->addItem($data);
        $result? $this->ajaxSuccess():$this->ajaxError(0,"操作失败");

    }



    protected function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
        $this->_model = null;
        $this->_model_role = null;
        $this->_model_role_party = null;
    }


}
